// index.js
// 获取应用实例
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import WxValidate from "../../utils/WxValidate"
import customerApi from '../../api/customer'
Page({
  data: {
    phone:'',
    name:"",
    employeeId:'',
    wxValidate:{}

  },

  onLoad(data) {
     //初始化验证参数
     this.initValidate()
     let id = data.id
     let name = data.name
     let phone = data.phone
     this.initData(id,name,phone)
     if(id) {
        wx.setNavigationBarTitle({
          title: "修改员工"
        })
     }

  },
  initData(id,name,phone) {
    this.setData({
      employeeId:id,
      name,
      phone
    })
  },
  initValidate() {
    const rules = {
       phone: {
        required: true,
        tel:true
      },
      name: {
        required: true,
      },
    }
    const messages = {
      phone: {
        required: "请输入手机号码"
      },
      name: {
        required: '请输入姓名'
      }
    }
    this.wxValidate = new WxValidate(rules, messages)
  
  },
  submitBtn(e) {
    console.log("---submitBtn-->",e.detail.value)
    if (!this.wxValidate.checkForm(e.detail.value)) {
      const error = this.wxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: "none",
        duration: 800
      })
      return false
    }
    let data = {
      ...e.detail.value,
      id:this.data.employeeId
    }
    customerApi.addCustomer(data).then(()=>{
      wx.showToast({
        title: this.data.employeeId ? "修改成功":'新增成功!',
        icon: "none",
        duration: 800
      })
      wx.navigateBack()
    })
  },
  delBtn() {
    Dialog.confirm({
      title: '温馨提示',
      message: '确定删除吗?',
    }).then(() => {
      customerApi.removeEmp(this.data.employeeId).then(()=>{
        wx.navigateBack()
      })
    }).catch(() => {
      // on cancel
    });;
  }

})
